package com.njym.controller;

import com.njym.annotation.PermInfo;
import com.njym.entity.SysApplyChannel;
import com.njym.entity.SysOrganization;
import com.njym.entity.SysPaymentMode;
import com.njym.entity.SysRole;
import com.njym.service.ApplyChannelService;
import com.njym.service.SysOrganizationService;
import com.njym.service.SysPaymentModeService;
import com.njym.service.SysRoleService;
import com.njym.vo.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * created by CaiBaoHong at 2018/4/17 16:41<br>
 */
@PermInfo(value = "选项模块", pval = "a:option")
@RestController
@RequestMapping("/option")
public class OptionController {

    private static final Logger log = LoggerFactory.getLogger(OptionController.class);

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysOrganizationService sysOrganizationService;

    @Autowired
    SysPaymentModeService sysPaymentModeService;

    @Autowired
    ApplyChannelService applyChannelService;

    @GetMapping("/role")
    public Json listRoleOptions() {
        String oper = "list role options";
        log.info(oper);

        EntityWrapper<SysRole> params = new EntityWrapper<>();
        params.setSqlSelect("rid,rname,rval");

        List<SysRole> list = sysRoleService.selectList(params);
        List<Option> options = list.stream().map(obj -> new Option(obj.getRid(), obj.getRname(), obj.getRval())).collect(Collectors.toList());
        return Json.succ(oper, "options", options);
    }

    @GetMapping("/org")
    public Json listOrgOptions() {
        String oper = "list org options";
        log.info(oper);
        EntityWrapper<SysOrganization> params = new EntityWrapper<>();
        params.setSqlSelect("id,org_name");
        params.eq("deleted",0);
        params.orderBy("sort_no");
        List<SysOrganization> list = sysOrganizationService.selectList(params);
        List<OrgVo> orgOptions = list.stream().map(obj -> new OrgVo(obj.getId(), obj.getOrgName())).collect(Collectors.toList());
        return Json.succ(oper, "orgOptions", orgOptions);
    }

    @GetMapping("/showAll")
    public Json showAll() {
        String oper = "show all options";
        log.info(oper);
        EntityWrapper<SysPaymentMode> params = new EntityWrapper<>();
        params.setSqlSelect("id,mode_name");
        params.eq("deleted",0);
        List<SysPaymentMode> list = sysPaymentModeService.selectList(params);
        List<PayVo> payOptions = list.stream().map(obj -> new PayVo(obj.getId(), obj.getModeName())).collect(Collectors.toList());
        System.out.println(payOptions.size()+"size");
        return Json.succ(oper, "payOptions", payOptions);
    }

    @GetMapping("/showChannel")
    public Json showChannel() {
        String oper = "show channel options";
        log.info(oper);
        EntityWrapper<SysApplyChannel> params = new EntityWrapper<>();
        params.setSqlSelect("id,channel_name");
        params.eq("deleted",0);
        List<SysApplyChannel> list = applyChannelService.selectList(params);
        List<ApplyChannelVo> acOptions = list.stream().map(obj -> new ApplyChannelVo(obj.getId(), obj.getChannelName())).collect(Collectors.toList());
        System.out.println(acOptions.size()+"size");
        return Json.succ(oper, "acOptions", acOptions);
    }
}
